package n2299;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Main2 {

	static int C[]=new int[500001];
	static int arr[]=new int[500001];
	static int input[]=new int[500001];
	static int lowbit(int x)
	{
		return x&-x;
	}
	static int MAX=500001;
	static int N;
	static void modify(int cur,int n)
	{
		for(int i=cur;i<=N;i+=lowbit(i))
		{
			C[i]+=n;
		}
	}
	static int sum(int cur)
	{
		int n=0;
		for(int i=cur;i>0;i-=lowbit(i))
		{
			n+=C[i];
		}
		return n;
	}
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{

		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		String line=null;
		Map<Integer,Integer> map=new HashMap<Integer,Integer>();//离散化处理输入数据
		while((line=bf.readLine())!=null)
		{
			Arrays.fill(C, 0);
			map.clear();
			int n=Integer.parseInt(line);
			if(n==0)break;
			N=n;
			int cnt=0;
			for(int i=0;i<n;i++)
			{
				line=bf.readLine();
				int k=Integer.parseInt(line);
				input[i]=k;
				if(!map.containsKey(k))
				{
					map.put(k, 0);
					arr[cnt++]=k;
				}
			}
			Arrays.sort(arr,0,cnt);
			for(int i=0;i<cnt;i++)
			{
				map.put(arr[i], i);
			}
			long count=0;
			for(int i=0;i<n;i++)
			{
				int sk=map.get(input[i]);
				int s=sum(sk+1);
				count+=(i-s);
				modify(sk+1, 1);
			}
			System.out.println(count);
		}
		
	}

}
